import {
  getMsgDetail,
  payOrder,
  passOrder,
  addOrder,
  cancalOrder
} from "../../utils/config"
import store from "../../utils/factory"
const util = require('../../utils/util.js')

const app = getApp()

Page({
  data: {
    detail: {}
  },
  onLoad: function (e) {
    console.log(e.id)
    this.getOrderDetail(e.id)
  },
  onShow() {

  },
  cancelOrder() {
    wx.showModal({
      title: "温馨提示",
      content: "是否确认取消订单",
      showCancel: true,
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          wx.showLoading({
            title: "取消订单中..."
          })
          cancalOrder({
            id: this.data.detail.id
          }).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: '取消订单成功',
              duration: 1000
            })
          }).catch(err => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  submit() {
    store.set("submitOrderDetail", this.data.detail)
    wx.redirectTo({
      url: '../submitOrder/submitOrder'
    })
  },
  pass() {
    wx.showModal({
      title: "温馨提示",
      content: "是否确认收货",
      showCancel: true,
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          passOrder({
            id: this.data.detail.id
          }).then(res => {
            wx.showToast({
              title: '确认收货成功',
              duration: 1000
            })
          })
        }
      }
    })
  },
  refund(e) {
    store.set("refundOrderDetail", this.data.detail)
    wx.redirectTo({
      url: '../refundOrder/refundOrder'
    })
  },
  getOrderDetail(id) {
    getMsgDetail({
      id: id
    }).then(res => {
      let req = res.data;
      req.createTime = util.formatTime(new Date(req.createTime));
      req.today = (req.cost / req.quantity).toFixed(2)
      this.setData({
        detail: req
      })
    })
  }
})